package cn.net.xyan.utils;

import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

/**
 * Created by zarra on 16/12/11.
 */
public class SplitCode {
    static final public String JavaExt = "java";


    public static class SplitCodeFileVisitor extends SimpleFileVisitor<Path> {

        boolean isSrc;

        PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:**.java");

        public SplitCodeFileVisitor(boolean isSrc) {
            this.isSrc = isSrc;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {

            if (!attrs.isDirectory()) {
                boolean isSrcFile = matcher.matches(file);
                if (isSrc) {

                    if (isSrcFile) {
                        //System.out.println(file);
                    } else {
                        System.out.println(file);
                        Files.delete(file);
                    }

                } else {
                    if (isSrcFile) {
                        System.out.println(file);
                        Files.delete(file);
                    } else {
                        //System.out.println(file);
                    }
                }
            }

            return FileVisitResult.CONTINUE;
        }
    }

    public static void walk(Path path,boolean isSrc) throws IOException {
        SplitCodeFileVisitor visitor = new SplitCodeFileVisitor(isSrc);

        java.nio.file.Files.walkFileTree(path, visitor);
    }

    public static void main(String[] args) throws IOException {

        String root = "/Users/zarra/IdeaProjects/finereport-parent/designer/src/main";

        Path pathJava     = Paths.get(root+"/java");
        Path pathResource = Paths.get(root+"/resources");
        walk(pathJava,true);
        walk(pathResource,false);
        return;
    }
}
